<?php
/*
  $Id: ot_kreditor_nor_payment_chg.php 2665 2005-11-30 22:41:21Z jb $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2002 osCommerce

  Released under the GNU General Public License
*/

class ot_kreditor_nor_payment_chg {
    var $title, $output;

    function ot_kreditor_nor_payment_chg() {
	$this->code = 'ot_kreditor_nor_payment_chg';
	$this->title = MODULE_KREDITOR_NOR_PAYMENT_CHG_TITLE;
	$this->description = MODULE_KREDITOR_NOR_PAYMENT_CHG_DESCRIPTION;
	$this->enabled = MODULE_KREDITOR_NOR_PAYMENT_CHG_STATUS;
	$this->sort_order = MODULE_KREDITOR_NOR_PAYMENT_CHG_SORT_ORDER;
	$this->tax_class = MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS;
	$this->output = array();
    }

    function process() {
	global $order, $ot_subtotal, $currencies;

	if ($_SESSION['payment'] != "kreditor_nor")
	    return;

	$od_amount = $this->calculate_credit($this->get_order_total());
	if ($od_amount != 0) {
	    $this->output[] = array('title' => $this->title . ':',
				    'text' => $currencies->format($od_amount),
				    'value' => $od_amount);
	    $order->info['total'] = $order->info['total'] + $od_amount;  
	    if ($this->sort_order < $ot_subtotal->sort_order) {
		$order->info['subtotal'] = $order->info['subtotal'] - $od_amount;
	    }
	}
    }
    

    function calculate_credit($amount) {
	global $order;

	$od_amount=0;

	if ($_SESSION['payment'] != "kreditor_nor") 
	    return $od_amount;

	if (MODULE_KREDITOR_NOR_PAYMENT_CHG_MODE == 'fixed') {
	    $od_amount = MODULE_KREDITOR_NOR_PAYMENT_CHG_FIXED;
	}
	else {
	    $table = split("[:,]" , MODULE_KREDITOR_NOR_PAYMENT_CHG_TABLE);

	    $size = sizeof($table);
	    for ($i=0, $n=$size; $i<$n; $i+=2) {
		if ($amount <= $table[$i]) {
		    $od_amount = $table[$i+1];
		    break;
		}
	    }
	}
	
	if ($od_amount == 0)
	    return $od_amount;

	if (MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS > 0) {
	    $tod_rate =zen_get_tax_rate(MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS);
	    $tod_amount = $od_amount - $od_amount/($tod_rate/100+1);
	    $order->info['tax'] += $tod_amount;
	    $tax_desc = zen_get_tax_description(
		MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS,
		$order->billing['country']['id'], $order->delivery['zone_id']);
	    $order->info['tax_groups']["$tax_desc"] += $tod_amount;
	}

 	if (DISPLAY_PRICE_WITH_TAX=="true") { 
 	    $od_amount = $od_amount;
 	} else {       
		$od_amount = $od_amount-$tod_amount;
 	    $order->info['total'] += $tod_amount;
 	}

	return $od_amount;
    }

   
    function get_order_total() {
	global  $order, $db;
	$order_total = $order->info['total'];

// Check if gift voucher is in cart and adjust total
	$products = $_SESSION['cart']->get_products();

	for ($i=0; $i<sizeof($products); $i++) {
	    $t_prid = zen_get_prid($products[$i]['id']);

	    $gv_result = $db->Execute(
		"select products_price, products_tax_class_id, ".
		"products_model from " . TABLE_PRODUCTS .
		" where products_id = '" . $t_prid . "'");

//	    $gv_result = tep_db_fetch_array($gv_query);

	

	    if (ereg('^GIFT', addslashes($gv_result->
					 fields['products_model']))) { 
		$qty = $_SESSION['cart']->get_quantity($t_prid);
		$products_tax =
		    zen_get_tax_rate($gv_result->
				     fields['products_tax_class_id']);

		if ($this->include_tax =='false') {
		    $gv_amount = $gv_result->fields['products_price'] * $qty;
		} else {
		    $gv_amount = ($gv_result->fields['products_price'] +
				  zen_calculate_tax(
				      $gv_result->fields['products_price'],
				      $products_tax)) * $qty;
		}
		$order_total=$order_total - $gv_amount;
	    }
	}

	if ($this->include_tax == 'false')
	    $order_total=$order_total-$order->info['tax'];

	if ($this->include_shipping == 'false')
	    $order_total=$order_total-$order->info['shipping_cost'];

	return $order_total;
    }

    
    function check() {
	global $db;
	if (!isset($this->check)) {
	    $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_KREDITOR_NOR_PAYMENT_CHG_STATUS'");
	    $this->check = $check_query->RecordCount();
	}

	return $this->check;
    }

    function keys() {
	return array('MODULE_KREDITOR_NOR_PAYMENT_CHG_STATUS',
		     'MODULE_KREDITOR_NOR_PAYMENT_CHG_MODE',
		     'MODULE_KREDITOR_NOR_PAYMENT_CHG_FIXED',
		     'MODULE_KREDITOR_NOR_PAYMENT_CHG_TABLE',
		     'MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS',
		     'MODULE_KREDITOR_NOR_PAYMENT_CHG_SORT_ORDER'
		     );
    }

    function install() {
	global $db;
	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_STATUS', 'true', 'Do you want to display the payment charge', '6', '1','zen_cfg_select_option(array(\'true\', \'false\'), ', now())");
	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_SORT_ORDER', '225', 'Sort order of display.', '6', '2', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Fixed invoice charge (inc. VAT)', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_FIXED', '0', 'Fixed invoice charge (inc. VAT). Use store default currency to calculate the invoice fee', '6', '7', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Charge Table', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_TABLE', '200:20,500:10,10000:5', 'The invoice charge is based on the total cost. Example: 200:20.500,10:10000:5,etc.. Up to 200 charge 20, from there to 500 charge 10, etc', '6', '2', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_TAX_CLASS', '0', 'Use the following tax class on the payment charge.', '6', '6', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Charge Type', 'MODULE_KREDITOR_NOR_PAYMENT_CHG_MODE', 'fixed', 'Invoice charge is fixed or based  on the order total.', '6', '0', 'zen_cfg_select_option(array(\'fixed\', \'price\'), ', now())");

    }

    function remove() {
	global $db;
	$keys = '';
	$keys_array = $this->keys();
	for ($i=0; $i<sizeof($keys_array); $i++) {
	    $keys .= "'" . $keys_array[$i] . "',";
	}
	$keys = substr($keys, 0, -1);

	$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
    }
}
?>